import { defineEventHandler, getRouterParam, createError } from 'h3'
import { fileUploadService } from 'server/utils/fileUpload'

export default defineEventHandler(async (event) => {
  try {
    // 获取文件ID
    const fileId = getRouterParam(event, 'id')
    if (!fileId) {
      throw createError({
        statusCode: 400,
        statusMessage: '文件ID不能为空'
      })
    }

    // 删除文件
    const deleteResult = await fileUploadService.deleteFile(parseInt(fileId))

    if (!deleteResult.success) {
      throw createError({
        statusCode: 400,
        statusMessage: deleteResult.error || '文件删除失败'
      })
    }

    return {
      success: true,
      message: '文件删除成功'
    }

  } catch (error: any) {
    console.error('删除文件错误:', error)
    
    if (error.statusCode) {
      throw error
    }

    throw createError({
      statusCode: 500,
      statusMessage: error.message || '服务器内部错误'
    })
  }
})